package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyThemeQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyESService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")

public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetail1Service;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisService strategyStatisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyESService strategyESService;


    @RequestMapping("")
    public String index(Model model){
        StrategyThemeQuery qo = new StrategyThemeQuery();
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        //推荐位
        model.addAttribute("commends",commends);

        //热门攻略 浏览数+评论数
        List<StrategyStatisVO> hotCds = strategyStatisService.queryHotTop10();
        model.addAttribute("hotCds",hotCds);
        //海外攻略
        List<StrategyStatisVO> abroadCds = strategyStatisService.queryAbroadTop10();
        model.addAttribute("abroadCds",abroadCds);
        //国内攻略
        List<StrategyStatisVO> unabroadCds = strategyStatisService.queryChinaTop10();
        model.addAttribute("unabroadCds",unabroadCds);

        //主题推荐
        List<Map<String,Object>> themeCds = strategyESService.queryThemeCommend();
        model.addAttribute("themeCds",themeCds);

        //国内条件: 通过省份id 跟省份名 分  : list<StatisVO>  vo(id, name, count)
        List<StatisVO> chinas = strategyESService.queryConditionChina();
        model.addAttribute("chinas", chinas);
        //国外条件: 通过国家id 国家的名 分
        List<StatisVO> abroads = strategyESService.queryConditionAbroad();
        model.addAttribute("abroads", abroads);

        //主题条件: 通过主题id 主题的名 分
        List<StatisVO> themes = strategyESService.queryConditionTheme();
        model.addAttribute("themes", themes);

        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){
        //page
        model.addAttribute("page", strategyESService.query(qo));

        return "strategy/searchPageTpl";
    }

    // TODO: 2020/3/26 阅读分数加一 热门
    // 阅读数
    @RequestMapping("/detail")
    public String detail(Model model, Long id){
        StrategyDetail strategyDetail = strategyDetail1Service.get(id);
        StrategyContent content = strategyDetail1Service.getContent(strategyDetail.getId());
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail",strategyDetail);
        //阅读数
        strategyStatisService.increaseViewNum(id,1);
        //共享vo
        StrategyStatisVO vo = strategyStatisService.getStrategyStatisVo(id);
        model.addAttribute("vo",vo);

        //阅读 排行加分
        strategyStatisService.addScoreHot(id,1);


        return "strategy/detail";
    }
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);
        PageInfo pageInfo = strategyDetail1Service.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }

    //// TODO: 2020/3/26  评论数 加分  热门
    //  评论数
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        strategyCommentService.saveOrUpdate(comment);

        //评论数
        strategyStatisService.increaseReplyNum(comment.getDetailId(),1);
        StrategyStatisVO vo = strategyStatisService.getStrategyStatisVo(comment.getDetailId());

        strategyStatisService.addScoreHot(comment.getDetailId(),1);
        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    // TODO: 2020/3/26 收藏分数加一 国内外
    // 收藏数 favor?sid=5
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            //请先登录 code = 102
            AjaxResult ajaxResult = new AjaxResult(false,"请先登录");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //收藏与取消收藏
        boolean ret = strategyStatisService.favor(sid,userInfo.getId());

        strategyStatisService.addScoreAbroadAndChina(sid,ret ? 1 : -1);


        return new AjaxResult(ret,"").addData(strategyStatisService.getStrategyStatisVo(sid));
    }

    // TODO: 2020/3/26 点赞分数加一   国内外
    //// 点赞数 strategyThumbup?sid=5
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            //请先登录 code = 102
            AjaxResult ajaxResult = new AjaxResult(false,"请先登录");
            ajaxResult.setCode(102);
            return ajaxResult;
        }

        //点赞与 已经点赞了
        boolean ret = strategyStatisService.strategyThumbup(sid,userInfo.getId());

        //加分
        strategyStatisService.addScoreAbroadAndChina(sid,ret ? 1 : -1);


        return new AjaxResult(ret,"").addData(strategyStatisService.getStrategyStatisVo(sid));
    }


    //评论的列表
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }



}
